package customerEnter.Frame.JDBCDao.SqlProcessing.SqlServer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import customerEnter.Frame.DaoDataTransmission.DaoFramUpdateOrAddMap;
import customerEnter.Frame.DaoDataTransmission.DaoFrameDMLBaseTransmission;
import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.DaoDataTransmission.SqlPrecompileModel;
import customerEnter.Frame.DaoDataTransmission.SqlPrecompileParameter;
import customerEnter.Frame.JDBCDao.Interface.DaoFrameSqlProcessingInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlPrecompileInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlPrecompileParameterInterface;
import customerEnter.Frame.JDBCDao.util.DaoFrameConntroller;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.Utils.javaReflexClass;
import customerEnter.tool.WhetherToEmpty;

/**
 * 类描述：處理 添加的批量語句
 * 
 * 类名称：DaoFrameSqlProcessingInsertImpl
 * 
 * 创建人：dy
 * 
 * 创建时间：2017-7-12 下午3:50:39
 * 
 */
public class DaoFrameSqlProcessingInsertBatchImpl implements DaoFrameSqlProcessingInterface<String> {

	@Override
	public <T, K> String SqlProcessing(DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		List<Object[]> objects = new ArrayList<Object[]>();
		try {
			if (daoFrameDMLBaseTransmission.isIsPreparedStatementParameter()) {
				objects = daoFrameDMLBaseTransmission.getListObjects();
				daoFramePameTransmission.SQL = DaoFrameConntroller.SqlManage(daoFrameDMLBaseTransmission.getDataBase(),
						daoFrameDMLBaseTransmission.getDML(), "", "", daoFrameDMLBaseTransmission.getTableName(), "");
				daoFramePameTransmission.setSqlPrecompileParameterInterface(InitlaztationListObjects(objects,
						new SqlPrecompileParameter()));
			} else {
				daoFramePameTransmission.setSqlPrecompileParameterInterface(InitlaztationdSqlPrecompileModel(
						mappingSql(daoFrameDMLBaseTransmission.getDataBase(),
								daoFrameDMLBaseTransmission.getTableName(), daoFrameDMLBaseTransmission.getValues(),
								daoFramePameTransmission), new SqlPrecompileParameter()));
			}
		} catch (Exception e) {
			Log4j.setLog4j(getClass());
			Log4j.Log4JOutError(e.getMessage());
		}
		return daoFramePameTransmission.SQL;
	}

	public <T, K> List<List<SqlPrecompileModel>> mappingSql(String DataBase, String TableName, List<Object> t,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) throws Exception {
		List<List<SqlPrecompileModel>> list = new ArrayList<List<SqlPrecompileModel>>();
		for (Object object : t) {
			List<SqlPrecompileModel> sqlPrecompileModels = new ArrayList<SqlPrecompileModel>();
			DaoFramUpdateOrAddMap daoFramUpdateOrAddMap = javaReflexClass.getObjectNamesAndValuesAdd(object);
			String fieldNames = "";
			String values = "";
			int i = 1;
			for (Entry<String, Object> entry : daoFramUpdateOrAddMap.getData().entrySet()) {
				SqlPrecompileModel sqlPrecompileModel = new SqlPrecompileModel();
				if (!WhetherToEmpty.StringWhetherToEmpty(fieldNames)) {
					fieldNames += ",";
					values += ",";
				}
				fieldNames += entry.getKey();
				values += "?";// entry.getValue();變成問號

				sqlPrecompileModel.setColumnName(entry.getKey());
				sqlPrecompileModel.setNumber(i);
				sqlPrecompileModel.setObject(entry.getValue());
				sqlPrecompileModels.add(sqlPrecompileModel);
				i++;
			}

			if (WhetherToEmpty.ObjectWhetherToEmpty(daoFramePameTransmission.SQL)) {
				daoFramePameTransmission.SQL = DaoFrameConntroller.SqlManage(DataBase, " insert ", "", "into",
						TableName + "(" + fieldNames + ")", "values(" + values + " ) ");
			}
			list.add(sqlPrecompileModels);
		}
		return list;
	}

	public SqlPrecompileParameterInterface InitlaztationListObjects(List<Object[]> objects,
			SqlPrecompileInterface sqlPrecompileInterface) {
		for (Object[] objects2 : objects) {

			int i = 1;
			for (Object object : objects2) {
				sqlPrecompileInterface.AddNumberValue(i, object);

				i++;
			}
			sqlPrecompileInterface.AddBatchSqlParameterModel();
		}
		return (SqlPrecompileParameterInterface) sqlPrecompileInterface;
	}

	public SqlPrecompileParameterInterface InitlaztationdSqlPrecompileModel(List<List<SqlPrecompileModel>> list,
			SqlPrecompileInterface sqlPrecompileInterface) {
		for (List<SqlPrecompileModel> list2 : list) {
			for (SqlPrecompileModel sqlPrecompileModel : list2) {
				sqlPrecompileInterface.AddNumberValue(sqlPrecompileModel.getColumnName(), sqlPrecompileModel
						.getNumber().intValue(), sqlPrecompileModel.getObject());
			}
			sqlPrecompileInterface.AddBatchSqlParameterModel();
		}
		return (SqlPrecompileParameterInterface) sqlPrecompileInterface;
	}
}
